package com.hsj.busmanager.service;

import com.hsj.busmanager.api.CheckUser;
import com.hsj.busmanager.api.CheckUserBean;
import com.hsj.busmanager.api.Cost;
import com.hsj.busmanager.api.UserBean;
import com.hsj.busmanager.mapper.CheckUserBeanMapper;
import com.hsj.busmanager.mapper.CheckUserMapper;
import com.hsj.busmanager.mapper.NewsMapper;
import com.hsj.busmanager.mapper.UserBeanMapper;
import com.hsj.busmanager.util.CommonUtil;
import com.hsj.busmanager.util.Page;
import com.hsj.busmanager.util.TabelData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserBeanMapper userMapper;

    @Autowired
    private CheckUserBeanMapper checkUserBeanMapper;

    @Autowired
    private CheckUserMapper checkUserMapper;

    @Autowired
   private NewsService newsService;

    public UserBean check(String userName, String password) {

       return  userMapper.selectByUserNameAndPassWd(userName,password);

    }

    public UserBean getUserById(Integer id) {
        return  userMapper.selectByPrimaryKey(id);
    }

    public void update(UserBean user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    public void saveCheckUsr(CheckUserBean user) {
        checkUserBeanMapper.insert(user);
    }

    public TabelData<UserBean> getDriverPage(Page page, UserBean user) {

        TabelData tabelData = new TabelData();

        Integer start = (page.getPage()-1)*page.getLimit();

        List<UserBean> userList = userMapper.getDriverPage(start,page.getLimit(),user);

        if(CommonUtil.listNotNull(userList)){
            for(UserBean userBean:userList){
                if(userBean.getDriverTime()!=null){
                    userBean.setShowDriverTime(CommonUtil.getDateByFormat(userBean.getDriverTime(),"yyyy-MM-dd"));
                }
            }
        }

        int count = userMapper.getDriverCount(user);

        if (userList == null && userList.size() == 0) {
            return null;
        }

        tabelData.setCount(count);
        tabelData.setData(userList);

        return tabelData;


    }

    public void deleteById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    public void insert(UserBean user) {
        userMapper.insert(user);
    }

    public TabelData<CheckUser> getUnCheckInfoPage(Page page, CheckUser user) {

        TabelData tabelData = new TabelData();

        Integer start = (page.getPage()-1)*page.getLimit();

        List<UserBean> userList = checkUserMapper.getUnCheckInfoPage(start,page.getLimit(),user);

        int count = checkUserMapper.getUnCheckInfoCount(user);

        if (userList == null && userList.size() == 0) {
            return null;
        }

        tabelData.setCount(count);
        tabelData.setData(userList);

        return tabelData;


    }

    @Transactional
    public void check(Integer id) {
        CheckUser checkUser = checkUserMapper.selectByPrimaryKey(id);

        UserBean user = new UserBean();
        BeanUtils.copyProperties(checkUser,user);

        //修改用户信息
        userMapper.updateByPrimaryKeySelective(user);

        //将审核用户表的信息删除
        checkUserMapper.deleteByPrimaryKey(id);

        //给用户发送审核信息成功邮件
        newsService.send("审核成功通知","您的个人修改资料已经审核通过!",6,id);
    }

    @Transactional
    public void checkFail(Integer id,String reason) {

        checkUserBeanMapper.deleteByPrimaryKey(id);

        newsService.send("审核失败通知","您的个人修改资料审核失败!原因为="+reason,6,id);
    }

    public List<UserBean> getDriverList() {
       return userMapper.getDriverList();
    }
}
